package com.examination.mlib.view;

import android.animation.ObjectAnimator;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.widget.NestedScrollView;

import com.examination.mlib.R;


public class ProtocalDialog extends Dialog {

    public ProtocalDialog(@NonNull Context context) {
        super(context);
    }

    public ProtocalDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected ProtocalDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder{
        private Context context;
        private String protocal_title;
        private SpannableStringBuilder protocal_message;
        private String disAgreeText;
        private String agreeText;
        private View contentView;
        private OnClickListener positiveButtonClickListener;
        private OnClickListener negativeButtonClickListener;
        private OnClickListener netuarlButtonClickListener;
        private View layout;
        private NestedScrollView protocalScroll;
        private ImageView protocalAnimView;
        private TranslateAnimation translateAni;

        private float [] x= {0f,60f,120f,180f,120f};
        private ObjectAnimator translationY;
        public Builder(Context context) {
            this.context = context;
        }

        public Builder setMessage(SpannableStringBuilder message) {
            this.protocal_message = message;
            return this;
        }

        /**
         * Set the Dialog message from resource
         *
         * @param
         * @return
         */
        public Builder setMessage(int message) {
            this.protocal_message = (SpannableStringBuilder) context.getText(message);
            return this;
        }

        /**
         * Set the Dialog title from resource
         *
         * @param title
         * @return
         */
        public Builder setTitle(int title) {
            this.protocal_title = (String) context.getText(title);
            return this;
        }

        /**
         * Set the Dialog title from String
         *
         * @param title
         * @return
         */

        public Builder setTitle(String title) {
            this.protocal_title = title;
            return this;
        }

        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }

        /**
         * Set the positive button resource and it's listener
         *
         * @param positiveButtonText
         * @return
         */
        public Builder setPositiveButton(int positiveButtonText,
                                         OnClickListener listener) {
            this.agreeText = (String) context
                    .getText(positiveButtonText);
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setPositiveButton(String positiveButtonText,
                                         OnClickListener listener) {
            this.agreeText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(int negativeButtonText,
                                         OnClickListener listener) {
            this.disAgreeText = (String) context
                    .getText(negativeButtonText);
            this.negativeButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(String negativeButtonText,
                                         OnClickListener listener) {
            this.disAgreeText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }

        public ProtocalDialog create() {
            translationY = ObjectAnimator.ofFloat(protocalAnimView, "translationY", x);
            translationY.setDuration(2000);
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // instantiate the dialog with the custom Theme
            final ProtocalDialog dialog = new ProtocalDialog(context, R.style.Dialog);
            layout = inflater.inflate(R.layout.dialog_protocal, null);
            dialog.addContentView(layout, new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            // set the dialog title
//            ((TextView) layout.findViewById(R.id.protocal_title)).setText(protocal_title);
            // set the confirm button

            if (agreeText != null) {
                ((TextView) layout.findViewById(R.id.protocal_agree))
                        .setText(agreeText);
                if (positiveButtonClickListener != null) {
                    ((TextView) layout.findViewById(R.id.protocal_agree))
                            .setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) {
                                    positiveButtonClickListener.onClick(dialog,
                                            DialogInterface.BUTTON_POSITIVE);
                                }
                            });
                }
            }
            else {
                // if no confirm button just set the visibility to GONE
                layout.findViewById(R.id.protocal_agree).setVisibility(
                        View.GONE);
            }
//            // set the cancel button
            if (disAgreeText != null) {
                ((TextView) layout.findViewById(R.id.protocal_disagree))
                        .setText(disAgreeText);
                if (negativeButtonClickListener != null) {
                    ((TextView) layout.findViewById(R.id.protocal_disagree))
                            .setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) {
                                    negativeButtonClickListener.onClick(dialog,
                                            DialogInterface.BUTTON_NEGATIVE);
                                }
                            });
                }
            }else {
                // if no confirm button just set the visibility to GONE
                layout.findViewById(R.id.protocal_disagree).setVisibility(
                        View.GONE);
            }
            // set the content message
            if (protocal_message != null) {
                TextView viewById = layout.findViewById(R.id.protocal_message);
                viewById.setMovementMethod(LinkMovementMethod.getInstance());
                viewById.setText(protocal_message);
//                RichText.from(protocal_message).bind(this)
//                        .showBorder(false)
//                        .size(ImageHolder.MATCH_PARENT, ImageHolder.WRAP_CONTENT)
//                        .into((TextView) layout.findViewById(R.id.protocal_message));
//                ((TextView) layout.findViewById(R.id.protocal_message)).setText(protocal_message);
            } else if (contentView != null) {
                // if no message set
                // add the contentView to the dialog body
                ((LinearLayout) layout.findViewById(R.id.content))
                        .removeAllViews();
                ((LinearLayout) layout.findViewById(R.id.content))
                        .addView(contentView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
            }
//            layout.findViewById(R.id.protocal_close).setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    //不同意退出登录
//                    boolean b = SpUtils.getInstance().removeAll(context);
//                    if (b) {
//                        ARouter.getInstance().build(ARouterConstants.CenterActivity).navigation();
//                        Activity context = (Activity) Builder.this.context;
//                        if (context != null && !context.isFinishing()) {
//                            context.finish();
//                        }
//                    }
//                }
//            });
            protocalScroll = (NestedScrollView) layout.findViewById(R.id.protocal_scroll);
            protocalScroll.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
                @Override
                public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                    layout.findViewById(R.id.protocal_animRelativeLayout).setVisibility(View.GONE);
                }
            });
            protocalAnimView = (ImageView) layout.findViewById(R.id.protocal_animView);

            translateAni = new TranslateAnimation(
                    Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT,
                    0, Animation.RELATIVE_TO_PARENT, 0,
                    Animation.RELATIVE_TO_PARENT, 0.25f);
            //设置动画执行的时间，单位是毫秒
            translateAni.setDuration(1000);
            translateAni.setRepeatCount(Animation.INFINITE);
            // 设置动画模式（Animation.REVERSE设置循环反转播放动画,Animation.RESTART每次都从头开始）
            translateAni.setRepeatMode(Animation.REVERSE);
            protocalAnimView.startAnimation(translateAni);
//            translationY.start();
            dialog.setContentView(layout);

            return dialog;
        }

    }





}
